//
//  CZWelcomeViewController.swift
//  CZ29VVeibo
//
//  Created by apple on 16/6/26.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SDWebImage
class CZWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 更改头像的约束
        // 使用update 约束的对象的view 没有发生改变 改变top 使用
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        /**
        动画
            - 01 - 动画执行时间
            - 02 - 延迟时间
            - 03 - 阻尼系数  范围 0-1  越大弹性效果越差
            - 04 - 起始速度
        
        - returns:
        */
        UIView.animateWithDuration(3, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            // 如果使用更改约束 frame 需要当前界面刷新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                       // 当前界面的动画完成了 改变根控制器  CZMainVc
                        // 发送通知 告知appdelegate 改变rootVc
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVcNoti, object: "welcome")
                })
                
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    // MARK: - 设置视图
    private func setupUI() {
        
        // 01- 添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        // 02- 添加约束(约束一定要添加全)
        headImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.top.equalTo(view).offset(400)
            make.centerX.equalTo(view)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
        
    }
    
    // MARK: - 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView(imgName: "ad_background")
    // 头像
    private lazy var headImageView: UIImageView = {
        let img = UIImageView(imgName: "avatar_default_big")
        // 设置layer
        img.layer.cornerRadius = 45
        img.layer.borderColor = ThemeColor.CGColor
        img.layer.borderWidth = 3
        img.layer.masksToBounds =  true
        // 设置图片
        img.sd_setImageWithURL(NSURL(string: CZUserAccountTools.sharedTools.userAccount?.avatar_large ?? ""), placeholderImage: UIImage(named: "ad_background"))
        return img
    }()
    // 欢迎回来
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        lab.text = "欢迎回来"
        lab.font = UIFont.systemFontOfSize(20)
        lab.textColor = UIColor.grayColor()
        // 对其方式
        lab.textAlignment = .Center
        // 透明度
        lab.alpha = 0
        return lab
    }()

}
